package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.CompanyProgressRankingAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.CompanyProgressRankEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompanyProgressRankActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.iv_name)
    ImageView ivName;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.rl_person)
    RelativeLayout rlPerson;
    @BindView(R.id.tv_hozom_company_name)
    TextView tvHozomCompanyName;
    @BindView(R.id.ll_hozom_sum)
    LinearLayout llHozomSum;
    @BindView(R.id.tv_sports_company_name)
    TextView tvSportsCompanyName;
    @BindView(R.id.ll_sports_times)
    LinearLayout llSportsTimes;
    @BindView(R.id.rl_title_label)
    RelativeLayout rlTitleLabel;
    @BindView(R.id.tv_contribution_title)
    TextView tvContributionTitle;
    @BindView(R.id.rv_ranking)
    RecyclerView rvRanking;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private CompanyProgressRankingAdapter rankingAdapter;

    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_company_progress_rank;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvRanking.setLayoutManager(layoutManager);
        rankingAdapter = new CompanyProgressRankingAdapter(this);
        rankingAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvRanking.setAdapter(rankingAdapter);
        rankingAdapter.getLoadMoreModule().setAutoLoadMore(false);
        initData();
        iniLoadMore();
    }

    private void iniLoadMore() {
        rankingAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        rankingAdapter.getLoadMoreModule().setAutoLoadMore(true);
        rankingAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        String string = "?limit=20&page=1";
        srl.setRefreshing(true);
        new ChallengeAPI().companyProgressRank(string, new BaseResultCallback<CompanyProgressRankEntity>() {
            @Override
            public void onResponse(CompanyProgressRankEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        rankingAdapter.setNewData(response.data);
                        if (rankingAdapter.getData().size() >= response.count) {
                            rankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                            rankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }
            }
        });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s";
        new ChallengeAPI().companyProgressRank(String.format(string, page), new BaseResultCallback<CompanyProgressRankEntity>() {
            @Override
            public void onResponse(CompanyProgressRankEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        rankingAdapter.addData(response.data);
                        if (rankingAdapter.getData().size() >= response.count) {
                            rankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                            rankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            rankingAdapter.getLoadMoreModule().loadMoreComplete();
                            rankingAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
